package com.qire.manhua.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.qire.common.basic.DateUtil;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.utils.NetworkUtils.NetworkType;
import com.qire.manhua.controller.ComicReaderController;
import com.qire.manhua.databinding.LayoutComicReaderBottomMenuBarBinding;
import com.qire.manhua.model.viewModel.IComicReaderViewModel;
import com.qire.common.receiver.NetWorkChangReceiver;
import com.qire.common.receiver.NetWorkChangReceiver.ConnectivityAction;

import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

/**
 * 漫画阅读页底部菜单栏
 */
public class ComicReaderBottomMenuBar extends LinearLayout {

    private final LayoutComicReaderBottomMenuBarBinding viewDataBinder = buildViewDataBinder(getContext());
    private IComicReaderViewModel comicReaderViewModel;

    private NetWorkChangReceiver netWorkChangReceiver;

    private LayoutComicReaderBottomMenuBarBinding buildViewDataBinder(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return LayoutComicReaderBottomMenuBarBinding.inflate(inflater);
    }

    public ComicReaderBottomMenuBar(Context context) {
        this(context, null);
    }

    public ComicReaderBottomMenuBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ComicReaderBottomMenuBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setOverScrollMode(VERTICAL);
        addView(viewDataBinder.getRoot());
        if(context instanceof LifecycleOwner) {
            viewDataBinder.setLifecycleOwner((LifecycleOwner) context);
        }

        netWorkChangReceiver = new NetWorkChangReceiver(connectivityAction);
        netWorkChangReceiver.registerReceiver(getContext());

        new Thread(timeTask).start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.comicReaderViewModel = null;
        netWorkChangReceiver.unregisterReceiver(getContext());
        isRunning = false;
    }

    public void setComicReaderController(ComicReaderController comicReaderController) {
        viewDataBinder.setComicReaderController(comicReaderController);
    }

    public void setComicReaderViewModel(IComicReaderViewModel comicReaderViewModel) {
        this.comicReaderViewModel = comicReaderViewModel;
        viewDataBinder.setComicReaderViewModel(comicReaderViewModel);

        setTimeText(DateUtil.Hm.currentTime());
        setReadProgressText("第?话 ?/?");
        setBulletText("", "#ffffff");

    }

    /**
     * 设置时间文本
     * @param timeText
     */
    private void setTimeText(CharSequence timeText) {
        if(ObjectUtil.nonNull(comicReaderViewModel)) {
            comicReaderViewModel.setTimeText(timeText);
        }
    }

    /**
     * 设置阅读进度文本
     * @param readProgressText
     */
    private void setReadProgressText(CharSequence readProgressText) {
        if(ObjectUtil.nonNull(comicReaderViewModel)) {
            comicReaderViewModel.setReadProgressText(readProgressText);
        }
    }

    /**
     * 设置网络状态显示
     * @param networkType
     */
    private void toggleNetworkPrompt(NetworkType networkType) {
        if(ObjectUtil.nonNull(comicReaderViewModel)) {
            comicReaderViewModel.setNetworkStatus(networkType.text, networkType.iconRid);
        }
    }

    /**
     * 更新弹幕文本及文本颜色
     * @param bulletText 弹幕文本
     * @param bulletTextColor 弹幕文本颜色
     */
    private void setBulletText(CharSequence bulletText, String bulletTextColor) {
        if(ObjectUtil.nonNull(comicReaderViewModel)) {
            comicReaderViewModel.updateBulletText(bulletText, bulletTextColor);
        }
    }


//    public String takeOutBulletScreenText(){
//        String bulletScreenText = contentText.get();
//        contentText.set("");
//        sendBtnTextColor.set("#aeaeae");
//        sendBtnClickable.set(false);
//        return bulletScreenText;
//    }

    private ConnectivityAction connectivityAction = new ConnectivityAction() {

        public boolean initNetworkStateFlag = true;

        @Override
        public void connected(NetworkType networkType) {
            toggleNetworkPrompt(networkType);
            if(initNetworkStateFlag) {
                initNetworkStateFlag = false;
                return;
            }
        }
        @Override
        public void disconnected(NetworkType networkType) {
            toggleNetworkPrompt(networkType);
        }

        @Override
        public void sendEvent(String eventMSG) {}
    };

    private volatile boolean isRunning = true;
    private Runnable timeTask = () -> {
        while(isRunning) {
            setTimeText(DateUtil.Hm.currentTime());
            long time = System.currentTimeMillis();
            long timeDifference = (60 * 1000) - time % (60 * 1000);
            try {
                Thread.sleep(timeDifference);
            } catch (InterruptedException e) {}
        }
    };

}
